package com.changgou.util;

import com.changgou.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * 实现FastDFS文件管理
 *      文件上传
 *      文件删除
 *      文件下载
 *      文件信息获取
 *      Storage信息获取
 *      Tracker信息获取
 */
public class FastDFSUtil {

    /**
     * 加载Tracker链接信息
     */
    //静态代码只执行一次
    static {
        try {
            //查找classpath下的文件路径
            String filename = new ClassPathResource("fdfs_client.conf").getPath();
            //加载Tracker链接信息
            ClientGlobal.init(filename);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 文件上传
     */
    public static String[] upload(FastDFSFile fastDFSFile) throws Exception {
        //附加参数
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] = new NameValuePair("author" , fastDFSFile.getAuthor());

        TrackerServer trackerServer = trackerServer();

        //第三步，通过TrackerServer的链接信息可以获取Storage的链接信息，创建StorageClient对象存储Storage的链接信息
        StorageClient storageClient = new StorageClient(trackerServer, null);

        //第四步，通过StorageClient访问Storage。实现文件上传，并且获取文件上传后存储信息
            //参数：1：上传文件按的字节数组；2：文件的扩展名；3：附加参数
            //uploads[]的返回值
                //1，文件上传所存储的Storage的组名字   group1
                //2，文件存储到Storage上的文件名字  M00/02/44/图片名字.jpg
         String[] uploads = storageClient.upload_file(fastDFSFile.getContent(), fastDFSFile.getExt(), meta_list);
         return uploads;
    }

    /**
     * 获取文件信息
     * @param groupName:文件的组名
     * @param remotrFileName：文件的存储路径名字
     */
    public static FileInfo getFiles(String groupName , String remotrFileName) throws Exception {

        TrackerServer trackerServer = trackerServer();

        //通过TrackerServer获取Storage信息，创建StorageClient对象存储Storage信息
        StorageClient storageClient = new StorageClient(trackerServer, null);

        //获取文件信息
        FileInfo fileInfo = storageClient.get_file_info(groupName, remotrFileName);

        //返回文件信息
        return fileInfo;
    }

    /**
     * 文件下载
     */
    public static InputStream downloadFile(String groupName , String remotrFileName) throws Exception{

        TrackerServer trackerServer = trackerServer();

        //通过TrackerServer获取Storage信息，创建StorageClient对象存储Storage信息
        StorageClient storageClient = new StorageClient(trackerServer, null);

        //下载文件
        byte[] downloadFile = storageClient.download_file(groupName, remotrFileName);

        //返回下载文件信息
        return new ByteArrayInputStream(downloadFile);
    }

    /**
     * 文件删除
     */
    public static int deleteFile(String groupName , String remotrFileName) throws Exception{

        TrackerServer trackerServer = trackerServer();

        //通过TrackerServer获取Storage信息，创建StorageClient对象存储Storage信息
        StorageClient storageClient = new StorageClient(trackerServer, null);

        //删除文件
        int deleteFile = storageClient.delete_file(groupName, remotrFileName);

        //返回删除的文件数
        return deleteFile;
    }

    /**
     * 获取Storage信息
     */
    public static StorageServer getStorages() throws  Exception{
        //创建一个TrackerClient对象，通过TrackerClient对象访问TrackerServer
        TrackerClient trackerClient = new TrackerClient();

        //通过TrackerClient获取TrackerServer的链接对象
        TrackerServer trackerServer = trackerClient.getConnection();

        //获取Storage信息
        StorageServer Storage = trackerClient.getStoreStorage(trackerServer);

        //返回Storage信息
        return Storage;
    }

    /**
     * 获取Storage的IP和端口信息
     */
    public static ServerInfo[] getServerInfo(String groupName , String remotrFileName) throws  Exception{
        //创建一个TrackerClient对象，通过TrackerClient对象访问TrackerServer
        TrackerClient trackerClient = new TrackerClient();

        //通过TrackerClient获取TrackerServer的链接对象
        TrackerServer trackerServer = trackerClient.getConnection();

        //获取Storage的IP和端口信息
        ServerInfo[] serverInfo = trackerClient.getFetchStorages(trackerServer, groupName, remotrFileName);

        //返回获取Storage的IP和端口信息
        return serverInfo;
    }

    /**
     * 获取Tracker信息
     */
    public static String getTrackerInfo() throws  Exception{

        TrackerServer trackerServer = trackerServer();

        //获取Tracker的IP,HTTP端口
        String ip = trackerServer.getInetSocketAddress().getHostName();
        int tracker_http_port = ClientGlobal.getG_tracker_http_port();

        //拼接Tracker路径
        String url = "http://" + ip + ":" + tracker_http_port;

        //返回Tracker路径
        return url;
    }

    /**
     * 获取TrackerServer
     */
    public static TrackerServer trackerServer() throws Exception{
        //创建一个TrackerClient对象，通过TrackerClient对象访问TrackerServer
        TrackerClient trackerClient = new TrackerClient();

        //通过TrackerClient获取TrackerServer的链接对象
        TrackerServer trackerServer = trackerClient.getConnection();

        return trackerServer;
    }

}
